package craky.componentc;

import java.awt.Color;
import java.awt.Component;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import com.abigdreamer.swing.component.UIUtil;

public class JCTabbedPane extends JTabbedPane
{
  private static final long serialVersionUID = 7830093625100770074L;
  private int tabHeight;
  private Color disabledForeground;
  private Map<Component, Color> disabledForegroundMap;

  public JCTabbedPane()
  {
    this(1, 0);
  }

  public JCTabbedPane(int tabPlacement)
  {
    this(tabPlacement, 0);
  }

  public JCTabbedPane(int tabPlacement, int tabLayoutPolicy)
  {
    super(tabPlacement, tabLayoutPolicy);
    setUI(new CTabbedPaneUI());
    setFont(UIUtil.getDefaultFont());
    setForeground(new Color(0, 28, 48));
    setBackground(Color.GRAY);
    setBorder(new EmptyBorder(0, 0, 0, 0));
    setOpaque(false);
    setTabLayoutPolicy(1);
    this.tabHeight = 24;
    this.disabledForeground = new Color(123, 123, 122);
    this.disabledForegroundMap = new HashMap();
  }

  public int getTabHeight()
  {
    return this.tabHeight;
  }

  public void setTabHeight(int tabHeight)
  {
    this.tabHeight = tabHeight;
    revalidate();
  }

  public Color getDisabledForeground()
  {
    return this.disabledForeground;
  }

  public void setDisabledForeground(Color disabledForeground)
  {
    this.disabledForeground = disabledForeground;
    repaint();
  }

  public Color getDisabledForegroundAt(int tabIndex)
  {
    Component c = getComponentAt(tabIndex);
    Color color = c == null ? null : (Color)this.disabledForegroundMap.get(c);
    return color == null ? this.disabledForeground : color;
  }

  public void setDisabledForegroundAt(int tabIndex, Color disabledForeground)
  {
    Component c = getComponentAt(tabIndex);

    if (c != null)
    {
      this.disabledForegroundMap.put(c, disabledForeground);

      if ((!isEnabledAt(tabIndex)) || (!isEnabled()))
      {
        repaint();
      }
    }
  }

  public void removeTabAt(int index)
  {
    Component c = getComponentAt(index);

    if (c != null)
    {
      this.disabledForegroundMap.remove(c);
    }

    super.removeTabAt(index);
  }

  public void setComponentAt(int index, Component component)
  {
    Component c = getComponentAt(index);
    Color color = null;

    if (c != null)
    {
      color = (Color)this.disabledForegroundMap.remove(c);
    }

    super.setComponentAt(index, component);

    if (color != null)
    {
      this.disabledForegroundMap.put(component, color);
    }
  }

  @Deprecated
  public void updateUI()
  {
  }
}